package org.roger.collector;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Scorer;

/**
 * 自定义收集器
 * 每个匹配的文档都会调用collect方法，对匹配的文档做进一步的处理
 * @author luoweiyi
 */
public class FilePathCollector implements LeafCollector{
	
	private Scorer scorer;
	
	//所有文档的path集合
	private String[] allPaths;
	//所有文档分数的集合
	private Float[] allScorers;
	
	//匹配文档的path几集合
	private List<String> paths = new ArrayList<String>();
	//匹配文档分数
	private List<Float> scorers = new ArrayList<Float>();

	public void setScorer(Scorer scorer) throws IOException {
		if(this.scorer == null)
			this.scorer = scorer;
	}

	public void collect(int doc) throws IOException {
		paths.add(allPaths[doc]);
		scorers.add(allScorers[doc]);
	}
	
	public List<String> getPaths(){
		return paths;
	}
	
	public List<Float> getScorers(){
		return scorers;
	}

	public boolean acceptsDocsOutOfOrder() {
		return true;
	}

	public LeafCollector getLeafCollector(LeafReaderContext leafreadercontext) throws IOException {
		int numDoc = leafreadercontext.reader().numDocs();
		allPaths = new String[numDoc];
		allScorers = new Float[numDoc];
		return this;
	}

	public boolean needsScores() {
		return false;
	}
}
